class Solution {
https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root==nullptr)
            return ret;
        queue<TreeNode*> q;
        q.push(root);
        
        bool flag=false;//标记是逆序
        while(!q.empty())
        {
            vector<int> tmp;
            int k=q.size();//记录pop K次
            
            while(k--)
            {
                TreeNode * t=q.front();
                tmp.push_back(t->val);
                q.pop();
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            if(flag)
                reverse(tmp.begin(),tmp.end());
            flag=!flag;
            ret.push_back(tmp);

        }
        return ret;
    }
};